﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Motoball.screenmanager
{
    public class MenuItem
    {
        public BasicModel model;
        public bool selectable = true;

        static float centerDistance = 500.0f;
        public Vector3 center = Vector3.Zero;

        bool hadOverflow = false;
        protected float rotation = 0.0f;
        float scale = 1.0f;
        public float scaleFactor = 0.09f;
        

        public MenuItem(BasicModel model)
        {
            this.model = model;
        }

        #region Methods
        public virtual void Draw(Camera camera, GameTime gameTime)
        {
            float rotationValue = Rotation;
            
            rotationValue /= MathHelper.Pi;
            
            model.World = Matrix.CreateScale(Scale);
            
            model.World *= Matrix.CreateRotationX(MathHelper.Lerp(MathHelper.Pi-1.0f,-MathHelper.PiOver2-0.2f,rotationValue));//rotationValue));
            if (rotationValue > 1)
                rotationValue = 2-rotationValue;
            
            foreach (ModelMesh mesh in model.model.Meshes)
            {
                
                    foreach (BasicEffect be in mesh.Effects)
                    {
                        //take mesh-color and multiply by position-value
                        be.DiffuseColor = model.diffColors[mesh.Name] * rotationValue;
                        be.SpecularColor = new Vector3(rotationValue / 6f);
                        be.SpecularPower = rotationValue + 1;            
                    }
                
            }
            model.World *= Matrix.CreateRotationZ(MathHelper.Lerp(0,-0.7f,rotationValue));
            model.World *= Matrix.CreateTranslation(0.0f, 0.0f, centerDistance);
            model.World *= Matrix.CreateTranslation(center);
            model.World *= Matrix.CreateRotationY(-Rotation);
            model.World *= Matrix.CreateTranslation(0, 0, centerDistance/2); 
            
            model.Draw(camera);
        }

        public virtual void Update(GameTime gameTime, float rotationRadian)
        {
            Rotation += rotationRadian;
           
            if (rotationRadian >= 0.0f)
            {
                if (Rotation >= MathHelper.Pi)
                    hadOverflow = true;
                else
                    hadOverflow = false;
            }
            else
            {
                if (Rotation <= MathHelper.Pi)
                    hadOverflow = true;
                else
                    hadOverflow = false;
            }

            Vector3 blup = model.World.Translation;
        }
        public bool FinishedRotation
        {
            get { return hadOverflow; }
        }
        #endregion

        #region Properties
        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        double exponent = 2.75;
        public virtual float Rotation
        {
            get { return rotation; }
            set
            {
                rotation = value;
                if (rotation >= MathHelper.TwoPi)
                    rotation -= MathHelper.TwoPi;
                else if (rotation < 0.0f)
                    rotation += MathHelper.TwoPi;

                if(rotation > MathHelper.Pi)
                    Scale = (float)Math.Pow((double)(MathHelper.TwoPi - Rotation), exponent) * scaleFactor;
                else
                    Scale = (float)Math.Pow((double)rotation, exponent)* scaleFactor;
                if (Scale < scaleFactor * 6f)
                    Scale = scaleFactor * 6f;
            }
        }
        #endregion
    }
}
